package com.example.demo.service;

import com.example.demo.common.UserSessionTools;
import com.example.demo.entity.ArticleInfo;
import com.example.demo.entity.UserInfo;
import com.example.demo.mapper.ArticleMapper;
import com.example.demo.service.ArticleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@EnableScheduling
public class ScheduleService implements SchedulingConfigurer {

    private long timer = 300000;//设置为5分钟
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    HttpServletRequest request;
    Logger logger = LoggerFactory.getLogger(ArticleService.class);
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //每5分钟执行一次
        taskRegistrar.addTriggerTask(new Runnable() {

            @Override
            public void run() {
                    LocalDateTime curtime = LocalDateTime.now();

                    //查询定时文章
                    logger.info("开始查询待发布文章");
                    List<ArticleInfo> articleInfos =  articleMapper.getTimeoutArticle();
                    //遍历查询结果看是否有需要发布的文章
                    for (ArticleInfo art: articleInfos) {
                        logger.info("正在查询待发布文章");
                        LocalDateTime publishtime = art.getPublishtime();
                        //表示 publishtime在curtime之前  发布文章
                        if (publishtime.isBefore(curtime)){
                            UserInfo userInfo = UserSessionTools.getLoginUser(request);
                            art.setUid(userInfo.getId());
                            art.setCreatetime(LocalDateTime.now());
                            //发布文章
                            logger.info("正在发布定时文章:"+art);
                            articleMapper.update(art);
                            /*
                 update articleinfo set title=#{title},content=#{content},updatetime=#{updatetime},state=1
        where id=#{id} and uid=#{uid}
                             */
                        }
                    }
                }

        }, new Trigger() {

            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {

                //此处使用不同的触发器，为设置循环时间的关键，区别于CronTrigger触发器，
                //该触发器可随意设置循环间隔时间，单位为毫秒
                PeriodicTrigger periodicTrigger = new PeriodicTrigger(timer);
                Date nextExecutionTime = periodicTrigger.nextExecutionTime(triggerContext);

                return nextExecutionTime;
            }
        });

    }
}